package com.bhkj.pdjhbgnewproject.common.utils;



import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Crypto {
    public static final String key = "abcdefgabcdefg12";
    public static final String iv = "abcdefgabcdefg12";

    public static void main(String[] args) throws Exception {
        String content = "张三010";
        String aa = content;//URLEncoder.encode(URLEncoder.encode(content, "UTF-8"), "UTF-8");
//        //加密  
        byte[] encrypted = AES_CBC_Encrypt(aa.getBytes(), key.getBytes(), iv.getBytes());

        System.out.println(encryptBASE64(encrypted));
//        
//        //解密  
//        byte[] decrypted=AES_CBC_Decrypt(decryptBASE64("Ys13OUVNYhUzGxYLya7kUg=="), key.getBytes(), iv.getBytes());   
//         
//        System.out.println("解密后："+byteArrayToHex(decrypted));  
//       System.out.println(byteToString(decrypted));  
        try {
            String BB = URLDecoder.decode("ynNINqseUGQPwnXgOIzNJsE84uwOgubeE00t8XQ2fbo%3D", "UTF-8");
            byte[] decrypted = Crypto.AES_CBC_Decrypt(Crypto.decryptBASE64(URLDecoder.decode(BB, "UTF-8")),
                    Crypto.key.getBytes(),
                    Crypto.iv.getBytes());
            String a = Crypto.byteToString(decrypted);
            System.out.println(URLDecoder.decode(a, "UTF-8"));
            System.out.println(a + "==========222===========");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public static String byteArrayToHex(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[byteArray.length * 2];
        for (int j = 0; j < byteArray.length; j++) {
            int v = byteArray[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static String byteToString(byte[] byte1) throws UnsupportedEncodingException {
        return new String(byte1, "UTF-8");
    }

    public static byte[] AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv) {

        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    public static byte[] AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv) {

        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catchblock   
            System.out.println("exception:" + e.toString());
        }
        return null;
    }

    /**
     * 字符串装换成base64
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) throws Exception {
        return Base64.decodeBase64(key.getBytes("UTF-8"));
    }

    /**
     * 二进制装换成base64
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] key) throws Exception {
        return new String(Base64.encodeBase64(key), "UTF-8");
    }

    /**
     * 解密
     *
     * @param mw
     * @return
     * @throws Exception
     */
    public static String decrypt(String mw) throws Exception {
        byte[] decrypted = AES_CBC_Decrypt(decryptBASE64(mw), key.getBytes(), iv.getBytes());
        return byteToString(decrypted);
    }

    /**
     * 加密
     *
     * @param mw
     * @return
     * @throws Exception
     */
    public static String encrypt(String mw) throws Exception {
        byte[] encrypted = AES_CBC_Encrypt(mw.getBytes("UTF-8"), key.getBytes(), iv.getBytes());
        return encryptBASE64(encrypted);
    }
}
